// const app = require('express')()

//导入数据库操作模块
const db = require("../../db/index.js")

// 格式化时间
var moment = require('moment')


/**
 * 增加图标
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.addIcon = (req, res) => {
    const Icon = req.body;
    if (JSON.stringify(Icon) == "{}") {
        console.log('【增加图标发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Icon.tag_name || !Icon.icon_url) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }

    //该图标是否已存在
    const findSql = 'SELECT * FROM xks_tags_icon WHERE status != 0 AND tag_name = ?'
    db.query(findSql, Icon.tag_name, (err, results) => {
        if (err) {
            console.log(err.message)
            return res.send({
                code: 505,
                msg: err.message,
                data: {}
            })
        }
        if (results.length > 0) {
            return res.send({
                code: 400,
                msg: '该图标（标题）已存在，请重新操作！',
                data: {}
            })
        }

        if (!Icon.create_time || Icon.create_time == undefined) {
            let nowTime = new Date()
            var formatTime = moment(nowTime).format('YYYY-MM-DD HH:mm')
            Icon['create_time'] = formatTime;
        } else {
            Icon.create_time = moment(Icon.create_time).format('YYYY-MM-DD HH:mm')
        }

        const addSql = `INSERT INTO xks_tags_icon SET ? `
        db.query(addSql, Icon, (err, result) => {
            if (err) {
                console.log(err.message)
                return res.send({
                    code: 505,
                    msg: err.message,
                    data: {}
                })
            }
            if (result.affectedRows !== 1) {
                return res.send({
                    code: 500,
                    msg: '增加失败，请稍后再试！',
                    data: {}
                })
            }
            res.send({
                code: 200,
                msg: '增加成功',
                data: {}
            })
        })
    })
}


/**
 * 删除图标
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.delIcon = (req, res) => {

    var Icon = req.query;
    if (JSON.stringify(Icon) == "{}") {
        console.log('【删除学习视频发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Icon.tag_name) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }

    const findSql = `SELECT * FROM xks_tags_icon WHERE status != 0 AND tag_name = ?`;
    db.query(findSql, Icon.tag_name, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '没有找到该数据',
                data: {}
            })
        }

        Icon = results[0]

        const delSql = `UPDATE xks_tags_icon SET status = 0 WHERE tag_name = ? `
        db.query(delSql, Icon.tag_name, (err, result) => {
            if (err) {
                console.log(err.message)
                return res.send({
                    code: 505,
                    msg: err.message,
                    data: {}
                })
            }
            if (result.affectedRows !== 1) {
                return res.send({
                    code: 500,
                    msg: '删除失败，请检查tag_name是否有误！',
                    data: {}
                })
            }
            res.send({
                code: 200,
                msg: '删除成功！',
                data: {}
            })
        })
    })
}


/**
 * 更新图标
 * @param {*} req 
 * @param {*} res 
 * @returns 
 */
exports.updateIcon = (req, res) => {
    const Icon = req.body;
    if (JSON.stringify(Icon) == "{}") {
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!Icon.tag_name || !Icon.icon_url) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }


    const updateSql = `UPDATE xks_tags_icon SET ? WHERE status != 0 AND tag_name = ?`
    db.query(updateSql, [Icon, Icon.tag_name], (err, result) => {
        if (err) {
            console.log(err.message)
            return res.send({
                code: 505,
                msg: err.message,
                data: {}
            })
        }
        if (result.affectedRows !== 1) {
            return res.send({
                code: 500,
                msg: '更新失败，请稍后再试！',
                data: {}
            })
        }
        res.send({
            code: 200,
            msg: '更新成功',
            data: {}
        })
    })
}